package com.zHan.controller;

import com.zHan.pojo.Result;
import com.zHan.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * ClassName: FileUploadController
 * Description:
 * date: 2023/11/13 0013 15:19
 *
 * @author zHan
 */
@RestController
public class FileUploadController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        // 获取原文件名
        String originalFilename = file.getOriginalFilename();
        // 保证文件名唯一
        String filename =
                UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = aliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success("图片上传成功", url);
    }
}
